package com.cy.store.repository;

import com.cy.store.entity.Cart;
import com.cy.store.entity.CartVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CartRepository extends JpaRepository<Cart, Integer> {

    //检验购物车中有无同款
    Cart findByUidAndPid(Integer uid, Integer pid);

    //显示购物车列表
    @Query("select new com.cy.store.entity.CartVO(c.cid, c.uid, c.pid, c.price, c.num, p.title, p.price, p.image) from Cart as c left join Product as p on c.pid = p.id where c.uid = ?1 order by c.createdTime desc ")
    List<CartVO> findCartVo(Integer uid);

    //根据勾选的cid列表获取商品信息
    //踩雷，@Param注解记得用import org.springframework.data.repository.query.Param;
    @Query("select new com.cy.store.entity.CartVO(c.cid, c.uid, c.pid, c.price, c.num, p.title, p.price, p.image) from Cart c left join Product p on c.pid=p.id where c.cid in :cids order by c.createdTime desc")
    List<CartVO> findVOByCids(@Param("cids")Integer[] cids);
}
